package com.frejoys.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frejoys.common.po.GoodsRepCountPo;
import com.frejoys.common.vo.app.GoodsVo;
import com.frejoys.common.vo.app.VerifyGoodsListVo;
import com.frejoys.dao.entity.Goods;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Set;

public interface GoodsMapper extends BaseMapper<Goods> {


    // 查询每个商家排第一的商品信息
    List<GoodsVo> selectStoreTopGoods(Set<Integer> storeIds);

    //赛选商品列表
//    List<GoodsVo> goodsList(GoodsSoldCountSort goodsSoldCountSort);


    /** 查询商品列表
     *
     * @param page
     * @param categoryIds 品类id
     * @param sort 排序: 默认0 1价钱降序 2价钱升序 3销售降序  4销售降序
     * @param status 上架状态: 0下架中，1申请上架，2上架中'
     * @param shelves 状态: 0无效，1有效'
     * @return
     */
    List<GoodsVo> goodsList(@Param("page") Page<Goods> page, @Param("categoryIds") List<Integer> categoryIds,@Param("sort") Integer sort,@Param("status") Integer status,@Param("shelves") Integer shelves);


    // 增加或减少指定商品id的商品库存
    int disposeGoodsRepCount(@Param("countPos") List<GoodsRepCountPo> countPos, @Param("ids") List<Integer> ids);


    // 获取上架状态
    @Select("select shelves,store_id from j_goods where id=#{id} limit 1")
    Goods getShelvesInfo(@Param("id") Integer commonId);


    // 更新上架状态
    @Update("update j_goods set shelves=#{shelves} where id=#{id}")
    Boolean upShelvesInfo(@Param("id") Integer id, @Param("shelves") Integer shelves);

    //根据店铺id查询商品
    List<VerifyGoodsListVo> verifyGoodsList(@Param("storeIds") List<Integer> storeIds,@Param("status") Integer status);

    //搜索商品
    @Select("SELECT id,store_id,name,cover,price,ori_price,sold_count,var_sold_count " +
            "FROM `j_goods` " +
            "WHERE `name` like concat(#{name},'%') and status = #{status} and shelves = #{shelves}")
    List<GoodsVo> selectGoodsList(@Param("page") Page page,@Param("name") String name,@Param("status") Integer status,@Param("shelves") Integer shelves);


    @Select("select distinct(category_id) as category_id from j_goods where store_id = #{id} and shelves = #{shelves} and status = #{status}")
    List<Integer> categoryIdInfo(@Param("id") Integer id,@Param("shelves") Integer shelves,@Param("status")Integer status);

    // 更新售出数量变量
    @Update("update j_goods set var_sold_count=var_sold_count+#{varSoldCount} where id=#{id}")
    boolean incrVarSoldCount(@Param("id") Integer id, @Param("varSoldCount") Integer varSoldCount);


    //根据商品名字查询商品id
    @Select("select id from j_goods where `name` like concat('%',#{name},'%')")
    List<Integer> getGoodsNameById(@Param("name") String name);

    // 店铺商品上架数量
    @Select("select count(*) from j_goods where store_id = #{storeId} and shelves =#{shelves} and status = #{status}")
    int validGoodsCount(@Param("storeId") Integer storeId, @Param("shelves") Integer shelves, @Param("status") Integer status);
}
